// pages/index/near-sites/index.js
const http = require('../../../utils/http.js')
const util = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '', // input实时的值
    name: '', // 模糊查询发给后台字段
    loading: false, // 是否显示加载动画
    hasMore: false, // 有无更多数据
    nearSiteList: [], // 附件站点
    nonearsite: false
  },
  deg: 0, // 动画旋转的角度
  page: 0, // 页码
  chargesiteObj: {}, // 充电站传递对象
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 节流函数优化搜索框
    this.loadData = util.throttle(this.inputSearch, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
		wx.getLocation({
			type: 'gcj02',
			success: (res) => {
				this.page = 0
				this.chargesiteObj = {
					page: this.page,
					size: 8,
					distance: true,
					latitude: res.latitude,
					longitude: res.longitude
				}
				// console.log(this.chargesiteObj)
				//  重置
				this.setData({
					nearSiteList: [],
					hasMore: false
				})

				http.get({ // 默认渲染第一页
					url: '/charge-site/index',
					data: this.chargesiteObj,
					success: (res) => {
						// console.log(res, '附近站点48行')
						if (res.statusCode === 200) {
							if (res.data.length === 0) {
								this.setData({
									nonearsite: true,

								});
								return false
							}
							this.setData({
								nearSiteList: this.data.nearSiteList.concat(res.data)
							})
							// console.log(this.data.nearSiteList)
						}
					}
				})
			}
		})

  },
  onShow() {

   
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // console.log('加载更多')
    this.nextPageSearch()
  },

  searchValueInput(e) { // 搜索框的值,实时事件
    this.loadData(e.detail.value)
  },

  // ---- 自定义事件 ----
  inputSearch(name) { // 搜索接口事件
    // console.log('name = ' + name)

    this.setData({
      name: name,
      hasMore: false
    })
    // 搜索第一次查询
    this.page = 0
    this.chargesiteObj = {
      page: this.page,
      size: 8,
      distance: true,
      latitude: this.chargesiteObj.latitude,
      longitude: this.chargesiteObj.longitude,
      name: name
    }
    // console.log(this.chargesiteObj)
    // // 默认渲染第一页
    http.get({
      url: '/charge-site/index',
      data: this.chargesiteObj,
      success: (res) => {
        // console.log(res, '搜索框第一页')
        if (res.statusCode === 200) {
          if (res.data.length === 0) {
            this.setData({
              nearSiteList: res.data,
              nonearsite: true
            })
            return false
          }
          this.setData({
            nearSiteList: res.data,
            nonearsite: false
          })
          // console.log(this.data.nearSiteList, '搜索事件出来的数据')
        }
      }
    })

  },

  nextPageSearch() { //上拉搜索事件
    if (this.data.loading) return;
    this.setData({
      loading: true
    });
    this.updateRefreshIcon();
    this.page++,
      // console.log(this.page, '上拉事件页码')
    this.chargesiteObj = {
      page: this.page,
      size: 8,
      distance: true,
      latitude: this.chargesiteObj.latitude,
      longitude: this.chargesiteObj.longitude,
      name: this.data.name
    }
    // console.log(this.chargesiteObj)
    setTimeout(() => {
      http.get({
        url: '/charge-site/index',
        data: this.chargesiteObj,
        success: (res) => {
          if (res.statusCode === 200) {
            if (res.data.length === 0) {
              this.setData({
                hasMore: true
              });
              this.setData({
                loading: false
              });
              return false
            }
            // this.updateRefreshIcon();
            this.setData({
              nearSiteList: this.data.nearSiteList.concat(res.data)
            })
            // console.log("站点数组长度=" + this.data.nearSiteList.length)
            this.setData({
              loading: false
            });
          }
        }
      })
    }, 200)
  },

  navToSite(e) { // 微信内置导航,传递参数为目标地点的经纬度
    const latitude = e.currentTarget.dataset.siteinfo.latitude
    const longitude = e.currentTarget.dataset.siteinfo.longitude
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 28
    })

  },

  updateRefreshIcon() { // 加载动画
    // console.log('旋转开始了.....')
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "linear",
      delay: 0
    });
    var timer = setInterval(() => {
      if (!this.data.loading) {
        // console.log(11)
        clearInterval(timer);
      }
      this.deg = this.deg += 360
      animation.rotateZ(this.deg).step(); //在Z轴旋转一个deg角度		
      this.setData({
        refreshAnimation: animation.export()
      })
    }, 1000);

  },

  navTositedetail(e) { // 跳转站点详情
    const siteId = e.currentTarget.dataset.siteid
    // console.log(siteId)
    wx.navigateTo({
      url: '../site-detail/index?siteId=' + siteId,
    })
  }
})